const { executeAsModal } = require("photoshop").core;

/**
 * 模块类型
 * @returns 
 */
const kersAPP = function () {
    return kersAPP;
}
kersAPP.lib = {}//库
kersAPP.layer = {};//图层
kersAPP.doc = {};//文档
/**
 * 模态执行函数
 * @param {*} fun Photoshop 进入模态状态后要执行的 JavaScript 函数
 * @param {*} commandName （必需）：描述命令的字符串。此字符串显示在进度栏 UI 中
 */
kersAPP.lib.runModalFunction = async function (fun, commandName) {
    await executeAsModal(fun, { "commandName": commandName });
}
/**
 * 通过图层id选中图层
 * @param {*} id 图层id
 */
kersAPP.layer.selectLayer = async function (id) {

    async function actionCommands() {

        const result = await batchPlay(
            [
                {
                    _obj: "select",
                    _target: [
                        {
                            _ref: "layer",
                            _id: id
                        }
                    ],
                    _options: {
                        dialogOptions: "dontDisplay"
                    }
                }
            ],
            {}
        );
    }

    await kersAPP.lib.runModalFunction(actionCommands, "Select Layer");

}
kersAPP.doc.closeDoc = async function (e) {
    async function actionCommands() {
        await e.save();
        await e.close();
    }
    await kersAPP.lib.runModalFunction(actionCommands, "close document");
}
const makeDefaultDocument = async () => {
  const result = await batchPlay(
    [
      {
        _obj: "make",
        new: {
          _obj: "document",
          artboard: false,
          autoPromoteBackgroundLayer: false,
          preset: "Default Photoshop Size",
        },
      },
    ],
    {}
  );

  return result[0];
};